//
// *  $Id: TestBlockIo.java,v 1.1 2000/05/06 00:00:53 boisvert Exp $
// *
// *  Unit tests for BlockIo class
// *
// *  Simple db toolkit
// *  Copyright (C) 1999, 2000 Cees de Groot <cg@cdegroot.com>
// *
// *  This library is free software; you can redistribute it and/or
// *  modify it under the terms of the GNU Library General Public License 
// *  as published by the Free Software Foundation; either version 2 
// *  of the License, or (at your option) any later version.
// *
// *  This library is distributed in the hope that it will be useful,
// *  but WITHOUT ANY WARRANTY; without even the implied warranty of
// *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// *  Library General Public License for more details.
// *
// *  You should have received a copy of the GNU Library General Public License 
// *  along with this library; if not, write to the Free Software Foundation, 
// *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
// 

//*************************************************************************
//**  Included in JDMB 1.0 port to SharpDBM;  11/2013 Cyrus Neah cneah@codingreal.com
//**  SharpDBM is an independent reimplementation of the JDBM 1.0 software library  
//*************************************************************************

using SharpDBM;
using SharpDBM.recman;
using SharpDBM.helper;
using SharpDBM.btree;

using NUnit.Framework;
using System.IO;
using System;
using System.Net;

namespace SharpDBM.Test

.recman
{

	

///
// *  This class contains all Unit tests for {@link BlockIo}.
// 
	[TestFixture]
    public class TestBlockIo  
	{

        //private  short SHORT_VALUE = 0x1234;
        //private  int INT_VALUE = 0xe7b3c8a1;
        //private  long LONG_VALUE = 0xfdebca9876543210L;

        private short SHORT_VALUE = 3700;
        private int INT_VALUE = 5;
        private long LONG_VALUE = 7;


		public TestBlockIo()  
		{
            
		}


//    *
//     *  Test writing
//     

      [Test]
	  public virtual void testWrite()
	  {
	  sbyte[] data = new sbyte[100];
	  BlockIo test = new BlockIo(0, data);
	  test.writeShort(0, (short)SHORT_VALUE);
	  test.writeLong(2, (long)LONG_VALUE);
	  test.writeInt(10, (int)INT_VALUE);
      byte[] bytes = Array.ConvertAll(data, b => (byte)b);

      

	 MemoryStream ms =  new MemoryStream(bytes); 
      BinaryReader @is = new BinaryReader(ms);
      //DataInputStream @is = 
      //new DataInputStream(new ByteArrayInputStream(bytes));
      //BufferedStream bs = new BufferedStream(new ByteArrayInputStream(bytes));


      if (BitConverter.IsLittleEndian)
      {
          NUnit.Framework.Assert.AreEqual(SHORT_VALUE, IPAddress.HostToNetworkOrder(@is.ReadInt16()), "short");
          NUnit.Framework.Assert.AreEqual(LONG_VALUE, IPAddress.HostToNetworkOrder(@is.ReadInt64()), "long");
          NUnit.Framework.Assert.AreEqual(INT_VALUE, IPAddress.HostToNetworkOrder(@is.ReadInt32()), "int");
      }
      else
      {
          NUnit.Framework.Assert.AreEqual(SHORT_VALUE, @is.ReadInt16(), "short");
          NUnit.Framework.Assert.AreEqual(LONG_VALUE, @is.ReadInt64(), "long");
          NUnit.Framework.Assert.AreEqual(INT_VALUE, @is.ReadInt32(), "int");
      }
		}

//    *
//     *  Test reading
//     


        [Test]
        public virtual void testRead()
		{
	  ByteArrayOutputStream bos = new ByteArrayOutputStream(100);
	  BinaryWriter os = new BinaryWriter(bos);

	  os.Write(SHORT_VALUE);
	  os.Write(LONG_VALUE);
	  os.Write(INT_VALUE);

      byte[] data = bos.ToByteArray();
      
     



      sbyte[] sdata = Array.ConvertAll(data, b => (sbyte)b);


	  BlockIo test = new BlockIo(0, sdata);


      if (BitConverter.IsLittleEndian)
      {
          NUnit.Framework.Assert.AreEqual(SHORT_VALUE, IPAddress.HostToNetworkOrder(test.readShort(0)), "short");
          NUnit.Framework.Assert.AreEqual(LONG_VALUE, IPAddress.HostToNetworkOrder(test.readLong(2)), "long");
          NUnit.Framework.Assert.AreEqual(INT_VALUE, IPAddress.HostToNetworkOrder(test.readInt(10)), ("int"));
      }
      else
      {
          NUnit.Framework.Assert.AreEqual(SHORT_VALUE, test.readShort(0), "short");
          NUnit.Framework.Assert.AreEqual(LONG_VALUE, test.readLong(2), "long");
          NUnit.Framework.Assert.AreEqual(INT_VALUE, test.readInt(10), ("int"));
      }
      
      
		}

        
//    *
//     *  Runs all tests in this class
//     
		
	}

}